const App = getApp();

Page({
  data: {
    // 搜索框样式
    searchColor: "rgba(0,0,0,0.4)",
    searchSize: "15",
    searchName: "搜索商品",

    // 列表高度
    scrollHeight: 0,

    curCategoryId1: 0, 
    curCategoryId2: 0, 
    // 分类列表
    leftlist: [],
    rightlist: [],

    ServerFileHost:'',
    categoryleveal:0
  },

  onLoad: function() {
    let _this = this;
    this.setData({
      ServerFileHost:App.ServerFileHost
    })
    // 设置分类列表高度
    _this.setListHeight();
    // 获取分类列表
    // _this.getCategoryList();
  },
  onShow:function(){
    this.getCategoryList();
  },
  /**
   * 设置分类列表高度
   */
  setListHeight: function() {
    let _this = this;
    wx.getSystemInfo({
      success: function(res) {
        _this.setData({
          scrollHeight: res.windowHeight - 47,
        });
      }
    });
  },

  /**
   * 获取分类列表
   */
  getCategoryList: function() {
    let _this = this;
    App._get('GetCategoryLists', {}, function(result) {
      let data = result.data.list;
      var leftlist = [];
      var rightlist = [];
      var categoryleveal = result.data.categoryleveal;
      if(categoryleveal === undefined)
        categoryleveal = 2;
      var curCategoryId1 = 0, 
        curCategoryId2 = 0;
      for(var i = 0; i< data.length; i++){
        var menu = {
          name : data[i].name,
          category_id : data[i].category_id,
          leveal : data[i].leveal,
          imgurl : data[i].imgurl,
          parcategory_id : 0
        }
        if(i == 0)
          curCategoryId1 = menu.category_id;
        if(categoryleveal == 1)
          rightlist.push(menu)
        else if(categoryleveal == 2){
          leftlist.push(menu)
          for(var j = 0; j < data[i].child.length; j++){
            var secondmenu = {
              name : data[i].child[j].name,
              category_id : data[i].child[j].category_id,
              leveal : data[i].child[j].leveal,
              imgurl : data[i].child[j].imgurl,
              parcategory_id : menu.category_id
            }
            rightlist.push(secondmenu);
          }
          if(i == 0)
            curCategoryId2 = menu.category_id
        }
        else if(categoryleveal == 3){
          leftlist.push(menu)
          menu.child = [];
          
          for(var j = 0; j < data[i].child.length; j++){
            var secondmenu = {
              name : data[i].child[j].name,
              category_id : data[i].child[j].category_id,
              leveal : data[i].child[j].leveal,
              imgurl : data[i].child[j].imgurl,
              parcategory_id : menu.category_id
            }
            if(i == 0 && j == 0)
              curCategoryId2 = secondmenu.category_id
            menu.child.push(secondmenu);
            for(var k = 0; k <  data[i].child[j].child.length; k++){
              var thidmenu = {
                name : data[i].child[j].child[k].name,
                category_id : data[i].child[j].child[k].category_id,
                leveal : data[i].child[j].child[k].leveal,
                imgurl : data[i].child[j].child[k].imgurl,
                parcategory_id : secondmenu.category_id
              }
              rightlist.push(thidmenu)
            }
          }
          
        }
      }
      _this.setData({
        leftlist: leftlist,
        rightlist: rightlist,
        curCategoryId1: curCategoryId1,
        curCategoryId2: curCategoryId2,
        categoryleveal:result.data.categoryleveal,
      });
    });
  },

  /**
   * 一级分类：选中分类
   */
  selectCate1: function(t) {
    let curCategoryId1 = t.target.dataset.id;
    var curCategoryId2 = 0;
    var menu = this.data.leftlist[t.target.dataset.index];
    if(this.data.categoryleveal == 2){
      curCategoryId2 = menu.category_id
    }
    else if(this.data.categoryleveal == 3){
      if(menu.child && menu.child.length > 0)
        curCategoryId2 = menu.child[0].category_id
    }
    
    this.setData({
      curCategoryId1,
      curCategoryId2,
      scrollTop: 0
    });
  },
  selectCate2: function(t) {
    let curCategoryId2 = t.target.dataset.id;
    this.setData({
      curCategoryId2,
      scrollTop: 0
    });
  },

  /**
   * 设置分享内容
   */
  onShareAppMessage: function() {
    return {
      title: "全部分类",
      path: "/pages/category/index"
    };
  }

});